﻿package com.victim.snake.user
{
	
	import com.victim.snake.snake.Snake;
	import com.victim.snake.snake.Body;
	import com.victim.snake.graphics.SnakeGraphics;
	import com.victim.snake.user.User;
	import com.victim.snake.user.ProcessMsg;
	
	import flash.system.System;
	import flash.net.Socket;
	import flash.utils.ByteArray;
	import flash.events.ProgressEvent; 
	import flash.display.MovieClip;
    import flash.events.Event;
	import flash.display.StageScaleMode;
	import flash.display.StageAlign;
	
	import fl.controls.Label;
	import flash.events.KeyboardEvent;
	import com.victim.snake.graphics.ErrorSprite;

	
	public class Main extends MovieClip
	{
		
		public function Main()
		{
			// 整个应用程序的大小固定,左上对齐
			this.stage.scaleMode=StageScaleMode.NO_SCALE;
			this.stage.align=StageAlign.TOP_LEFT;
			System.useCodePage=true;
			
			var socket:Socket = new Socket();//连接服务器
			//trace(socket.timeout);
			
			socket.addEventListener(Event.CONNECT,ProcessMsg.begin);	//添加以连接事件;			
			socket.addEventListener(ProgressEvent.SOCKET_DATA,ProcessMsg.socketRead);	//添加接收到服务器端发送消息的事件;
			socket.addEventListener(flash.events.IOErrorEvent.IO_ERROR,connectionError);	//添加IO错误事件
			socket.addEventListener(flash.events.SecurityErrorEvent.SECURITY_ERROR,securityError);	//添加安全错误事件
			socket.connect("127.0.0.1",8787);

			//将socket和stage添加到User的静态变量中，以便其它类使用
			User.setSocket(socket);
			User.setStage(this.stage);
			User.setErrorSprite(new ErrorSprite());
			User.getErrorSprite().visible=false;
			User.getStage().addChildAt(User.getErrorSprite(),1);
		}
		
		
		//IO错误事件
		public function connectionError(e:flash.events.IOErrorEvent):void{
			User.getErrorSprite().setErrorString("连接错误，请刷新页面重新登陆");
			User.getErrorSprite().visible=true;	//显示消息					
		}
		
		//安全错误事件
		public function securityError(e:flash.events.SecurityErrorEvent):void{
			User.getErrorSprite().setErrorString("安全错误，请刷新页面并重新登陆");
			User.getErrorSprite().visible=true;	//显示消息					
		}



	}

}